package com.zephyr.vchcenter.pbcFile.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.log4j.Logger;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import com.zephyr.vchcenter.bean.BaseObject;
import com.zephyr.vchcenter.pbcFile.commons.Constance;
import com.zephyr.vchcenter.pbcFile.exception.MatchException;

/**
 * @author zhaoyunxiao
 * �ļ��������
 */
public class FileParserUtil {
	private static final Logger log = Logger.getLogger(FileParserUtil.class);

	public static boolean getRequestData(String str, String regex) {
		Pattern p = Pattern.compile(regex);
		Matcher matcher = p.matcher(str);
		boolean flag = matcher.find();
		return flag;
	}
    //针对上海文件将两行分割成为一行
	public static List<String> splitToSingle(String str) {
		List<String> list = new ArrayList<String>();
		String tempLine1 = str.substring(0,
				Constance.SHANGHAI_TOWLINE_DIVIDELENTH);
		String tempLine2 = str
				.substring(Constance.SHANGHAI_TOWLINE_DIVIDELENTH);
		list.add(tempLine1);
		list.add(tempLine2);
		return list;
	}

	//
	public static String getStandardSingle(String str, String regex) {
		Pattern pattern = Pattern.compile(regex);
		Matcher matcher = pattern.matcher(str);
		StringBuffer stringBuffer = new StringBuffer();
		if (matcher.find()) {
			for (int i = 2; i < matcher.groupCount() + 1; i++) {
				stringBuffer.append(matcher.group(i));
			}
		}
		return stringBuffer.toString();
	}

	//按行对去文件并装入集合中
	public static List<String> readFileByLine(File file) {
		List<String> list = new ArrayList<String>();
		try {
			InputStream inputStream = new FileInputStream(file);
			BufferedReader reader = new BufferedReader(new InputStreamReader(
					inputStream));

			while (true) {
				String temp = reader.readLine();
				list.add(temp);
				if (temp == null) {
					break;
				}
			}
		} catch (FileNotFoundException e) {
			log.error("FileParserUtil", e);
		} catch (IOException e) {
			log.error("FileParserUtil", e);
		}
		return list;

	}
	//根据给定的值从给定的某行读取文件
	public static List<String> getRequestLines(List<String> list,int beginLineNum){
        log.debug("FileParserUtil::getRequestLines");
        if(beginLineNum>list.size()-1){
        	throw new MatchException("��ƥ����ݣ�");
        }
		List<String> temp=list.subList(beginLineNum, list.size()-1);
		List<String> returnValue=new ArrayList<String>();;
		for(String str:temp){
			
        	if(str!=null&&str.trim().length()>3&&getRequestData(str.trim().substring(0,3), "[\\d]{1}")){
        		returnValue.add(str);
        	}
        }
		
		return returnValue;
	}

	//利用反射的机制将人行数据装入业务对象
	public static BaseObject encapsulation(BaseObject object,
			Map<String, Object> map) {
		BaseObject baseObjectReturn = null;
		try {
			Class baseObjectClass = object.getClass();
		
			Method[] methods = baseObjectClass.getDeclaredMethods();
			Field[] fields = baseObjectClass.getDeclaredFields();
			for (Field field : fields) {
				if (map.containsKey(field.getName())) {
					for (Method method : methods) {
						if ((method.getName().toLowerCase()).equals("set"
								+ field.getName().toLowerCase())) {
							method.invoke(object, (String)map.get(field.getName()));
						}
					}
				}
			}
			baseObjectReturn =object;
		} catch (Exception e) {
			log.error("FileParserUtil:encapsulation:error", e);
		}
		return baseObjectReturn;
	}
	public String getDecoder(String str){
		BASE64Decoder base64decoder=new BASE64Decoder();
		//BASE64Encoder be=new BASE64Encoder();
		String ss=null;
		try{
			byte[] b=base64decoder.decodeBuffer(str);
			String s=new String(b,"utf-8");
			byte[] bb=s.getBytes("utf-8");
			ss=s;//new String(bb,"utf-8");
			log.debug(s);
		} catch (IOException e) {
              log.error("文件编码错误");
		}
		return ss;
	}
	/*public static void main(String[] args) {
		//new FileParserUtil().getDecoder("5o+Q5YWl6KGM5ouS56Wo5riF5Y2V5o+Q5YWl6KGM6ZO26KGM5Y+3OiAxMTQ0Njc3IOWcuuasoTogIDEg5pel5pyfOiAyMDA4LTEwLTE1IOW4geenjTrkurrmsJHluIHmiZPljbDml7bpl7Q6IDIwMDgtMTAtMTQgMjE6MDA6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